package com.todod.common.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONObject;

import cn.dev33.satoken.stp.StpUtil;

@Component
public class LtCloudUtil {

	@Value("${ltcloud.manage.url}")
	private String BASE_URL;

	@Autowired
	private RestTemplate restTemplate;

	/**
	 * @Title: getUserList
	 * @Description: 获取用户列表，用来针对不同的云平台的用户显示
	 * @author: bzq
	 * @date 2023-07-26
	 * @param userId
	 * @return JSONObject
	 **/
	public JSONObject getUserList(Long userId) {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		headers.add(StpUtil.getTokenName(), StpUtil.getTokenValue());
		ResponseEntity<String> postForEntity = restTemplate
				.postForEntity(BASE_URL + "/ltcloud/user/getUserList?userId=" + userId, headers, String.class);
		JSONObject reData = JSONObject.parseObject(postForEntity.getBody());
		return reData;
	}

	/**
	 * @Title: getTableListByDataSource
	 * @Description: 获取指定的数据源中所包含的所有表信息
	 * @author: bzq
	 * @date 2023-07-31
	 * @param userId
	 * @param datasourceId
	 * @return JSONObject
	 **/
	public JSONObject getTableListByDataSource(Long userId, Long datasourceId) {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		headers.add(StpUtil.getTokenName(), StpUtil.getTokenValue());
		ResponseEntity<String> postForEntity = restTemplate.postForEntity(
				BASE_URL + "/maDataSource/getTableList?userId=" + userId + "&id=" + datasourceId, headers,
				String.class);
		JSONObject reData = JSONObject.parseObject(postForEntity.getBody());
		return reData;
	}

}
